<?
    include "../common.php" ;
    $opt = $_POST['opt'] ;
    $arr = [] ; 
    if( $opt == "BTypePage" ){
        $perPage = $_POST['perPage'] ;
        if( $_POST['typeId'] == 'faType' ){
            $sql = "SELECT COUNT(*) AS c FROM `type` WHERE subtypeid IS null" ;
        } 
        if( $_POST['typeId'] == 'subType' ) {
            $sql = "SELECT COUNT(*) AS c FROM `temper` WHERE is_Delete = 1 " ;
        }
        
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr['totalPage'] = ceil($one['c']/$perPage) ;
        }
    }
    if( $opt == 'selUpt' ){
        $typeId = $_POST['typeId'] ;
        $sql = "SELECT * FROM `type` WHERE id = {$typeId}" ;
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr = $one ;
        }
    }
    if( $opt == "updBType" ){
        $typeName = $_POST['typeName'] ; 
        $typeId = $_POST['typeId'] ; 
        $sql = "UPDATE `type` SET `typeName` = '{$typeName}'  WHERE id = {$typeId}" ; 
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '修改成功' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败' ;
            $arr['sql'] = $sql ;
        }
    }
    if( $opt == "delBType" ){
        $typeId = $_POST['typeId'] ;
        $sql = "DELETE FROM `type` WHERE id = {$typeId} OR subtypeid = {$typeId}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '删除成功' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败' ;
        }
    }
    if( $opt == "selBType" ){
        $sql = "SELECT * FROM `type` WHERE subtypeid IS null" ;
        $res = mysqli_query($link,$sql);
        $str = "" ;
        while( $one = mysqli_fetch_assoc($res) ){
            $str .= "<option value = '{$one["id"]}'>{$one['typeName']}</option>" ;
        }
        $arr['data'] = $str ; 
    }
    if( $opt == 'getSTypeList' ){
        $page = $_POST['page'] ;
        $perPage = $_POST['perPage'] ;
        $pageSt = ( $page - 1 ) * $perPage ; 
        $sql = "SELECT * FROM `temper` WHERE is_Delete = 1 ORDER BY id ASC LIMIT $pageSt,$perPage" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ;
        }
    }
    
    if( $opt == 'updSelStype' ){
        $typeId = $_POST['typeId'] ; 
        $sql = "SELECT * FROM `temper` WHERE id = {$typeId} " ;
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr = $one; 
        }
    }
    if( $opt == 'delStype' ){
        $typeId = $_POST['typeId'] ;
        $sql = "DELETE FROM `temper` WHERE id = {$typeId}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '删除成功' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败' ;
        }
    }
    if( $opt == 'addTransImg' ){
        $now =Time();
        $str = date('Ymd',Time()) ;
        $isTrans = $_POST['isTrans'] ; 
        if(!file_exists("../../uploads/{$str}")){     //看是否存在文件夹uploads
            mkdir("../../uploads/{$str}");
        }
        $path = $_FILES['transImg']['tmp_name'];
        move_uploaded_file($path,"../../uploads/{$str}/".$_FILES['transImg']['name']);
        $arr = $_POST ; 
        $transImg = "uploads/{$str}/".$_FILES['transImg']['name'] ; 
        $sql = "INSERT INTO `transimg`(`transImg`, `isTrue`) VALUES ( '{$transImg}' , {$isTrans} )" ; 
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '添加成功！' ;
        } else{
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ; 
        }
    }
    if($opt == 'getImgList'){
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ; 
        $pageSt = ( $page - 1 ) * $perPage ;
        $sql = "SELECT * FROM `transimg` WHERE isDelete = 1 ORDER BY id ASC LIMIT $pageSt,$perPage" ; 
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ;
        } 
    } 
    if( $opt == 'uptImg' ){
        $operate = $_POST['operate'] ; 
        $imgId = $_POST['imgId'] ;
        if( $operate == 'isTrue' ){
            $sql = "SELECT * FROM `transimg` WHERE id = {$imgId} " ;
            $res = mysqli_query($link,$sql);
            $one = mysqli_fetch_assoc($res);
            // var_dump($one);die;
            if( $one['isTrue'] == 1 ){
                $imgStatus = 0 ;
            }
            if( $one['isTrue'] == 0 ){
                $imgStatus = 1 ;
            }
            $sql1 = " UPDATE `transimg` SET `isTrue` = $imgStatus WHERE id = {$imgId} " ;
            
            $res1 = mysqli_query($link,$sql1);
            if( $res1 ){
                $arr['status'] = 1 ;
                $arr['info'] = '操作成功' ; 
            } else {
                $arr['status'] = 0 ;
                $arr['info'] = '操作失败' ; 
            }
        }
        if( $operate == 'isDelete' ){
            $sql = " UPDATE `transimg` SET `isDelete` = 2 WHERE id = {$imgId} " ;
            $res = mysqli_query($link,$sql);
            if( $res ){
                $arr['status'] = 1 ;
                $arr['info'] = '删除成功！' ; 
            } else {
                $arr['status'] = 2 ;
                $arr['info'] = '操作失败！' ; 
            }
        }
    }
    if( $opt == 'getImgPageNum' ){
        $perPage = $_POST['perPage'] ;
        $sql = "SELECT COUNT(*) AS c FROM `transimg` WHERE isDelete = 1 ";
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr['totalPage'] = ceil( $one['c']/$perPage ) ; 
        } 
    }
    if( $opt == 'getAllImg' ){
        $sql = "SELECT * FROM `transimg` WHERE isTrue = 1 AND isDelete = 1";
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ;
        }  
    }
    if( $opt == 'addGoods' ){
        $pname = $_POST['pname'] ; 
        $pDescribe = $_POST['pDescribe'] ;
        $BType = $_POST['BType'] ; 
        $subType1 = $_POST['subType1'] ; 
        $price = $_POST['price'] ; 
        $inventory = $_POST['inventory'] ; 
        $now =Time();
        $str = date('Ymd',Time()) ;
        $isTrans = $_POST['isTrans'] ; 
        if(!file_exists("../../images/{$str}")){     //看是否存在文件夹images
            mkdir("../../images/{$str}");
        }
        $path = $_FILES['showImg']['tmp_name'];
        move_uploaded_file($path,"../../images/{$str}/".$_FILES['showImg']['name']); 
        $ImgSrc = "images/{$str}/".$_FILES['showImg']['name'] ;
        $sql = "INSERT INTO `products`(`typeId`,`temperId`, `pName`, `pDescribe`, `price`, `showImg`, `inventory`) VALUES 
        ({$BType},{$subType1},'{$pname}','{$pDescribe}',{$price},'{$ImgSrc}',{$inventory})" ;
        // echo $sql ; die; 
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ; 
            $arr['info'] = '添加成功！' ;
        } else{
            $arr['status'] = 0 ; 
            $arr['info'] = '操作失败！' ;
        }
    }
    if( $opt == 'getProList' ){
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ; 
        $pageSt = ( $page - 1 ) * $perPage ; 
        $sql = "SELECT p.id AS id,pName,pDescribe,price,showImg,inventory,typeName,temperName FROM `products` AS P JOIN `type` AS t ON p.typeId = t.id JOIN temper AS tp ON tp.id = p.temperId ORDER BY id ASC LIMIT $pageSt,$perPage";
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'updProEle' ){
        $pid = $_POST['pid'] ;
        $sql  = "SELECT p.id AS id,t.id AS typeid,pName,pDescribe,price,showImg,inventory,typeName,temperId FROM `products` AS P JOIN `type` AS t ON p.typeId = t.id JOIN temper AS tp ON tp.id = p.temperId  WHERE p.id = {$pid}" ;
        $res = mysqli_query($link,$sql);
        // echo $sql; die ;
        $newArr1 = [] ;
        $newArr2 = [] ;
        $str1 = '' ;
        $str2 = '' ;
        $one = mysqli_fetch_assoc($res) ;
        $sql1 = "SELECT * FROM `type` WHERE subtypeid IS NULL";
        $res1 = mysqli_query($link,$sql1);
        while( $one1 = mysqli_fetch_assoc($res1) ){
            $newArr1[] = $one1 ; 
        }
        foreach( $newArr1 as $key => $value ){
            if( $one['typeid'] == $value['id'] ){
                $str1 .= "<option value='{$value["id"]}' selected>{$value['typeName']}</option>";
            } else {
                $str1 .= "<option value='{$value["id"]}'>{$value['typeName']}</option>";
            }
        }
        $sql2 = "SELECT * FROM `temper` WHERE is_Delete = 1 ";
        $res2 = mysqli_query($link,$sql2);
        while( $one2 = mysqli_fetch_assoc($res2) ){
            $newArr2[] = $one2 ; 
        }
        foreach( $newArr2 as $key => $value ){
            if( $one['temperId'] == $value['id'] ){
                $str2 .= "<option value='{$value["id"]}' selected>{$value['temperName']}</option>";
            } else {
                $str2 .= "<option value='{$value["id"]}'>{$value['temperName']}</option>";
            }
        }
        
        $one['data1'] = $str1 ; 
        $one['data2'] = $str2 ;
        $arr = $one ; 
    }
    if( $opt == 'updatePro' ){
        $pid = $_POST['pid'];
        $pname = $_POST['pname'];
        $BType = $_POST['BType'];
        $subType = $_POST['subType'];
        $pDescribe = $_POST['pDescribe']; 
        $price = $_POST['price'];
        $inventory = $_POST['inventory'];
        $sql = "UPDATE `products` SET `typeId`={$BType},temperId={$subType},`pName`='{$pname}', `pDescribe`='{$pDescribe}',
        `price`={$price},`inventory`={$inventory} WHERE id = {$pid}" ; 
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '修改成功！' ;
        } else{
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败！' ;
        }
    }
    if( $opt == 'delProEle' ){
        $pid = $_POST['pid'] ; 
        $sql = "DELETE FROM products WHERE id = {$pid}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = "删除成功！" ; 
        }  else{
            $arr['status'] = 0 ;
            $arr['info'] = "操作失败！" ; 
        }
    }
    if( $opt == 'getPagePro' ){
        $perPage = $_POST['perPage'] ;
        $sql = "SELECT COUNT(*) AS c FROM `products` AS P JOIN `type` AS t ON p.typeId = t.id JOIN temper AS tp ON tp.id = p.temperId" ;
        $res = mysqli_query($link,$sql);
        $one = mysqli_fetch_assoc($res);
        $arr['totalPage'] = ceil( $one['c'] / $perPage );
    }
    if( $opt == 'temperOpt' ){
        $temperId = $_POST['temperId'] ; 
        $sql = "SELECT * FROM temper WHERE id = {$temperId}" ;
        // echo $sql ; die ; 
        $res = mysqli_query($link,$sql) ;
        $one = mysqli_fetch_assoc($res) ;
        if( $one['is_show'] == 1 ){
            $is_show = 2 ; 
        } else {
            $is_show = 1 ; 
        }
        $sql1 = "UPDATE `temper` SET is_show = {$is_show} WHERE id = {$temperId}" ;
        // echo $sql1 ; die ;
        $res1 = mysqli_query($link,$sql1) ;
        if( $res1 ){
            $arr['status'] = 1 ; 
            $arr['info'] = "修改成功！" ;
        } else {
            $arr['status'] = 0 ; 
            $arr['info'] = "操作失败！" ;
        }
    }
    echo json_encode($arr);
?>